using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using Object = System.Object;

namespace IQIGame.Onigao.Logic
{
    public class SkillEffective_InsertSkill : SkillEffective
    {
        protected override ISerializeCustomPOD OnExecute(SkillEffectiveData data, Unit target, SkillEffectiveContext context, int effectiveIndex, out SkillEffectiveResponse startResp)
        {
            startResp = null;
            if (data.effectiveConfig.customConfig is SkillEffective_InsertSkill_ConfigData rConfig)
            {
                var nSkillID = rConfig.skillID;
                if (nSkillID < 0)
                {
                    nSkillID = context.skill.skillID;
                }
                this.InsertSkill(nSkillID, target);
            }

            return null;
        }

        protected override ISerializeCustomPOD OnActionExecute(SkillEffectiveData data, SkillActionNode action, Unit target, ref SkillEffectiveContext context, int effectiveIndex)
        {
            if (action.customConfig is SkillEffectiveAction_InsertSkill_ConfigData rConfig)
            {
                var rAttacker = context.skill.executer;
                var nSkillID = rConfig.skillID.GetValue(rAttacker, target, context);
                if (nSkillID < 0)
                {
                    nSkillID = context.skill.skillID;
                }
                this.InsertSkill(nSkillID, target);
            }

            return null;
        }

        private void InsertSkill(int nSkillID, Unit rTarget)
        {
            var rSkillComp = rTarget.GetComponent<SkillComponent>(ETComponentType.SkillComponent);
            if (rSkillComp.skillDict.TryGetValue(nSkillID, out var rSkill))
            {
                rTarget.battle.playCtrl.queueCtrl.InsertSkill(rSkill);
            }
            else
            {
                LogGame.LogError($"尝试插入了自己未拥有的技能{nSkillID}");
            }
        }

#if UNITY_EDITOR
        public override string ToString(SkillCustomConfig config)
        {
            var rConfig = config as SkillEffective_InsertSkill_ConfigData;
            return $"插入技能{rConfig.skillID}";
        }
#endif
    }
}